﻿using Book.Data;
using Book.Entities;
using Book.IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Book.Repositiories
{
    public class AchievementsRepository: IAchievementsRepository
    {
        private readonly BookDbContext _DbContext;

        public AchievementsRepository(BookDbContext bookDbContext)
        {
            this._DbContext = bookDbContext;
        }
        /// <summary>
        /// 添加成果
        /// </summary>
        /// <param name="title"></param>
        /// <param name="abstracts"></param>
        /// <param name="year"></param>
        /// <param name="link"></param>
        /// <param name="code"></param>
        /// <param name="category"></param>
        public void AddAchievements(string title, string abstracts, string year, string link, string code,string category)
        {
            AchievementsEntity achievements = new AchievementsEntity
            {
                Id = Guid.NewGuid().ToString("N").Substring(0, 29),
                Abstracts = abstracts,
                Year = year,
                Link = link,
                Code = code,
                Title = title,
                Category=category
            };
            _DbContext.achievements.Add(achievements);
            _DbContext.SaveChanges();
        }
        /// <summary>
        /// 获得全部成果
        /// </summary>
        /// <returns></returns>
        public List<AchievementsEntity> GetAchievements()
        {
            List<AchievementsEntity> achievementsList = _DbContext.achievements.ToList();
            return achievementsList;
         }
        /// <summary>
        /// 根据类别查找成果
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public List<AchievementsEntity> GetAchievementsByCategory(string category)
        {
            List<AchievementsEntity> achievementsList = _DbContext.achievements.Where(x => x.Category == category).ToList();
            return achievementsList;
         }
        /// <summary>
        /// 根据类别获得年份
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public List<string> GetYearByCategory(string category)
        {
            List<string > achievementsList = _DbContext.achievements.Where(x => x.Category == category).Select(x=>x.Year).Distinct().OrderByDescending(x => x).ToList();
            return achievementsList;
        }
        public List<AchievementsEntity> GetAchievementsByYear(string year)
        {
            List<AchievementsEntity> achievementsList = _DbContext.achievements.Where(x => x.Year == year).ToList();
            return achievementsList;
        }
        /// <summary>
        /// 删除成果
        /// </summary>
        /// <param name="id"></param>
        public void DeleteAchievements(string id)
        {
            AchievementsEntity achievementsEntity = _DbContext.achievements.Find(id);
            _DbContext.achievements.Remove(achievementsEntity);
            _DbContext.SaveChanges();
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <param name="abstracts"></param>
        /// <param name="year"></param>
        /// <param name="link"></param>
        /// <param name="code"></param>
        /// <param name="category"></param>
        public void UpdateAchievements(string id, string title, string abstracts, string year, string link, string code, string category)
        {
            AchievementsEntity achievementsEntity = _DbContext.achievements.Find(id);
            achievementsEntity.Title = title;
            achievementsEntity.Abstracts = abstracts;
            achievementsEntity.Year = year;
            achievementsEntity.Link = link;
            achievementsEntity.Code = code;
            achievementsEntity.Category = category;
            _DbContext.achievements.Update(achievementsEntity);
            _DbContext.SaveChanges();
        }
    }
}
